package com.tikeyc.loveshare.activity;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.alibaba.mobileim.IYWLoginService;
import com.alibaba.mobileim.YWAPI;
import com.alibaba.mobileim.YWIMKit;
import com.alibaba.mobileim.YWLoginParam;
import com.alibaba.mobileim.channel.event.IWxCallback;
import com.tikeyc.loveshare.R;
import com.tikeyc.loveshare.myApplication.MyApplication;

public class TLoginActivity extends AppCompatActivity {

    private YWIMKit mIMKit;

    private EditText username_et;
    private EditText password_et;
    private TextView loginButton_tv;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);

        username_et = (EditText) findViewById(R.id.username_et);
        password_et = (EditText) findViewById(R.id.password_et);

        loginButton_tv = (TextView) findViewById(R.id.loginButton_tv);
        loginButton_tv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                login();
            }
        });


        String userid = "testpro1";
        username_et.setText(userid);
        String password = "taobao1234";
        password_et.setText(password);
    }


    /**
     *
     */
    private void login() {
        //开始登录
        final String userid = username_et.getText().toString();
        String password = password_et.getText().toString();

        MyApplication myApplication = (MyApplication) getApplication();
        //此对象获取到后，保存为全局对象，供APP使用
        //此对象跟用户相关，如果切换了用户，需要重新获取
        mIMKit = YWAPI.getIMKitInstance(userid, MyApplication.APP_KEY);
        if (mIMKit == null) {
            Toast.makeText(this,"用户名错误",Toast.LENGTH_SHORT).show();
            return;
        }
        myApplication.mIMKit = mIMKit;
        IYWLoginService loginService = mIMKit.getLoginService();
        YWLoginParam loginParam = YWLoginParam.createLoginParam(userid, password);
        loginService.login(loginParam, new IWxCallback() {

            @Override
            public void onSuccess(Object... arg0) {
//                Intent intent = mIMKit.getConversationActivityIntent();
//                startActivity(intent);
                Intent intent = new Intent(TLoginActivity.this, TMainFragmentTabActivity.class);
                TLoginActivity.this.startActivity(intent);
                TLoginActivity.this.finish();
            }

            @Override
            public void onProgress(int arg0) {
                // TODO Auto-generated method stub
            }

            @Override
            public void onError(int errCode, String description) {
                Log.e("TAG",description);
                //如果登录失败，errCode为错误码,description是错误的具体描述信息
            }
        });
    }
}
